<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/27
 * Time: 21:50
 */
require "./vendor/overtrue/wechat/autoload.php";
require "./vendor/autoload.php";
use Overtrue\Wechat\Server;
use Overtrue\Wechat\Message;
use GuzzleHttp\Client;

$appId          = '';
$token          = 'boram';
$encodingAESKey = 'rWyXRSNQg6eCSbTgRQyF6PLjje0ER5AuGc4AjsUm5ZI'; // 可选

// $encodingAESKey 可以为空
$server = new Server($appId, $token, $encodingAESKey);

$server->on('message','text',processCouplets($message));

function processCouplets($message)
{
    if (strpos($message,"嵌名对联") !== false)
    {
        return Message::make('text')->content('请按照“姓名+性别”的格式发送您的信息');
    }
    elseif (strpos($message,'+') !== false)
    {
        $message = trim($message);
        $name = explode('+',$message)[0];
        $sex = explode('+',$message)[1];
        $client = new Client(['base_uri'=>'.\couplet.php']);
        $response = $client->get('?name='.$name.'&sex='.$sex);
        $couplets = $response->getBody();
        $couplets = explode('|',$couplets);
        $string = '';
        foreach($couplets as $value)
        {
            $string .= $value.'\n';
        }
        return Message::make('text')->content($string);
    }
    else
    {
        return Message::make('text')->content('请输入“签名对联”获取嵌名对联');
    }
}


